package py.edu.ucom.cadira.net;

import java.util.concurrent.LinkedBlockingQueue;

public class ThreadWorkerPool {

	public static final int POOL_SIZE=10;
	private NetListenerServer netListenerServer;
	private LinkedBlockingQueue<XMLSerializable> queue;
	
	private boolean keepAlive;
	
	public ThreadWorkerPool(NetListenerServer netListenerServer){
		this.queue=new LinkedBlockingQueue<XMLSerializable>();
		this.keepAlive=true;
		this.netListenerServer=netListenerServer;
	}
	public void pushTask(XMLSerializable xmlSerializable){
		this.queue.add(xmlSerializable);
	}
	
	public synchronized void shutDown(){
		this.keepAlive=false;
	}
	public void start(){
	
		for(int i=0;i<POOL_SIZE;i++){
			new Thread(new Worker()).start();
		}
	}
	
	
	private class Worker implements Runnable{

		public void run() {
			
			while(keepAlive){
				try {
					XMLSerializable xml = queue.take();
					netListenerServer.messageArrived(xml);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
		}
		
	}
}
